<?php
session_start();
require('conn.php');
require('functions.php');
//echo $session['phone'];

if ($_GET['is_find']) {
	// $row_user=$mysql->query("select * from `userdata` where `id` in({$_SESSION['userdata']['id']})");

	$row=$mysql->query("select * from `userdata` where phone={$_GET['phone']} limit 1");
	// var_dump($row);
	// var_dump($_GET['phone']);
	if (!$row) {
	print <<<alert
	<script>
	alert('非注册用户无法找回密码');
	</script>
alert;
exit;
	}
}else{
	$row=$mysql->query("select * from `userdata` where phone={$_GET['phone']} limit 1");
	if ($row) {
			print <<<alert
			<script>
			alert('当前手机号已被注册');
			</script>
alert;
exit;
}
}
if(time()-$_COOKIE['yzm_time']<=60){
print <<<alert
	<script>
	alert('1分钟内只能获取一次验证码，请不要重复操作');
	</script>
alert;
exit;
}else{
	setcookie('yzm',$rand,time()+60);
	setcookie('yzm_time',time(),time()+60);
print <<<alert
	<script>
	alert('验证码已发送到您手机，5分钟内有效');
	</script>
alert;
}

	$msg_ip = $config["msg_ip"]; 
	$msg_port = $config["msg_port"]; 
	$uid = $config["msg_uid"]; 
	$pwd = md5($config["msg_pwd"]);
	$mobile = $_GET["phone"]; 
	$srcphone = $config["msg_srcphone"]; 
	$yzm1=$_SESSION['yzm1'];
	$msg="【米柚科技】您的验证码是{$yzm1}";
	// var_dump($config);
    echo send_sms($msg_ip, $msg_port, $uid, $pwd, $mobile, $srcphone, $msg);

//手机短信
function send_sms($msg_ip, $msg_port, $uid, $pwd, $mobile, $srcphone, $msg){
	$encoded_msg=urlencode("$msg");
	$url="http://$msg_ip:$msg_port/cmppweb/sendsms?uid=$uid&msg=$encoded_msg&srcphone=$srcphone&mobile=$mobile&pwd=$pwd";
	$post_string="uid=$uid&msg=$encoded_msg&srcphone=$srcphone&mobile=$mobile&pwd=$pwd";
	return sock_post($url, $post_string);
}


/**
* url 为服务的url地址
* query 为请求串
*/
function sock_post($url,$query){
	$data = "";
	$info=parse_url($url);
	$fp=fsockopen($info["host"],$info["port"],$errno,$errstr,30);
	if(!$fp){
		return $data;
	}
	$head="POST ".$info['path']." HTTP/1.0\r\n";
	$head.="Host: ".$info['host']."\r\n";
	$head.="Referer: http://".$info['host'].":".$info["port"].$info['path']."\r\n";
	$head.="Content-type: application/x-www-form-urlencoded\r\n";
	$head.="Content-Length: ".strlen(trim($query))."\r\n";
	$head.="\r\n";
	$head.=trim($query);
	$write=fputs($fp,$head);
	$header = "";
	while ($str = trim(fgets($fp,4096))) {
		$header.=$str;
	}
	while (!feof($fp)) {
		$data .= fgets($fp,4096);
	}
	return $data;
}
?>